package com.sa.token.message;

import cn.hutool.core.util.StrUtil;
import lombok.Data;

import static com.sa.token.constant.Constants.EMPTY;

/**
 * @desc: 标准响应报文，在调用接口服务时统一响应的报文信息
 * @author: xhs
 * @date: 2021/8/20 13:32
 * @version: JDK 1.8
 */
@Data
public class StandardResponse<T> {

    /**
     * 返回状态码
     */
    private Integer status;

    /**
     * 返回msg
     */
    private String msg;

    /**
     * 返回的业务ID
     */
    private String businessId;

    /**
     * 返回的数据
     */
    private T data;

    public StandardResponse() {
    }

    public StandardResponse(Integer status, String msg) {
        this(status, msg, EMPTY);
    }

    public StandardResponse(Integer status, String msg, String businessId) {
        this.status = status;
        this.msg = msg;
        this.businessId = businessId;
    }

    public StandardResponse(Integer status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public StandardResponse(Integer status, String msg, String businessId, T data) {
        this.status = status;
        this.msg = msg;
        this.businessId = businessId;
        this.data = data;
    }

    /**
     * @param result
     * @param <T>
     * @return code msg
     */
    public static <T> StandardResponse<T> build(Result result) {
        return new StandardResponse<T>(result.getCode(), result.getMsg());
    }

    /**
     * 返回状态码,msg,业务ID
     *
     * @param businessId
     * @param result
     * @param <T>
     * @return code msg businessId
     */
    public static <T> StandardResponse<T> build(String businessId, Result result) {
        return new StandardResponse<T>(
                result.getCode(),
                result.getMsg(),
                businessId);
    }

    /**
     * 返回状态码,msg,数据
     *
     * @param result
     * @param data
     * @param <T>
     * @return code msg data
     */
    public static <T> StandardResponse<T> build(Result result, T data) {
        return new StandardResponse<T>(
                result.getCode(),
                result.getMsg(),
                data);
    }

    /**
     * 返回状态码、msg、业务ID、数据
     *
     * @param businessId
     * @param result
     * @param data
     * @param <T>
     * @return code msg businessId data
     */
    public static <T> StandardResponse<T> build(String businessId, Result result, T data) {
        return new StandardResponse<T>(
                result.getCode(),
                result.getMsg(),
                businessId,
                data);
    }

    /**
     * 返回成功
     *
     * @param <T>
     * @return code msg businessId data
     */
    public static <T> StandardResponse<T> success() {
        return new StandardResponse(Result.SUCCESS.getCode(), Result.SUCCESS.getMsg(), "", "");
    }

    /**
     * 返回成功 传msg
     *
     * @param msg
     * @param <T>
     * @return code msg businessId data
     */
    public static <T> StandardResponse<T> success(String msg) {
        return new StandardResponse(Result.SUCCESS.getCode(), msg, "", "");
    }

    /**
     * 返回成功、传数据
     *
     * @param data
     * @param <T>
     * @return code msg businessId data
     */
    public static <T> StandardResponse<T> success(T data) {
        return new StandardResponse(Result.SUCCESS.getCode(), Result.SUCCESS.getMsg(), "", data);
    }

    /**
     * 返回成功,传业务ID、msg
     *
     * @param businessId
     * @param msg
     * @param <T>
     * @return code msg businessId data
     */
    public static <T> StandardResponse<T> success(String businessId, String msg) {
        return new StandardResponse<>(Result.SUCCESS.getCode(), StrUtil.isBlank(msg)
                ? Result.SUCCESS.getMsg() : msg, businessId);
    }

    /**
     * 返回成功,传msg、数据
     *
     * @param data
     * @param <T>
     * @return code msg businessId data
     */
    public static <T> StandardResponse<T> success(String msg, T data) {
        return new StandardResponse<>(Result.SUCCESS.getCode(), msg, "", data);
    }

    /**
     * 返回失败
     *
     * @param <T>
     * @return code msg businessId data
     */
    public static <T> StandardResponse<T> error() {
        return new StandardResponse<>(Result.ERROR.getCode(), Result.ERROR.getMsg());
    }

    /**
     * 返回失败,传，msg
     *
     * @param msg
     * @param <T>
     * @return code msg businessId data
     */
    public static <T> StandardResponse<T> error(String msg) {
        return new StandardResponse<>(Result.ERROR.getCode(), msg);
    }

    /**
     * 返回失败的code、msg
     *
     * @param code 状态码
     * @param msg  msg
     * @param <T>
     * @return code msg businessId data
     */
    public static <T> StandardResponse<T> error(Integer code, String msg) {
        return new StandardResponse(code, msg);
    }

    /**
     * 判断当前是否为处理成功的响应
     *
     * @return
     */
    public boolean successful() {
        return Result.SUCCESS.getCode().equals(this.status);
    }

    /**
     * 设置返回的数据
     *
     * @param t
     * @return
     */
    public StandardResponse<T> setData(T t) {
        this.data = t;
        return this;
    }

}
